package org.ww.product.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.R;
import org.ww.product.entity.AttrEntity;
import org.ww.product.entity.AttrGroupEntity;
import org.ww.product.service.AttrAttrgroupRelationService;
import org.ww.product.service.AttrGroupService;
import org.ww.product.service.AttrService;
import org.ww.product.service.CategoryService;
import org.ww.product.vo.AttrGroupRelationVo;
import org.ww.product.vo.AttrGroupWithAttrVo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author ww
 * @email ww@qq.com
 * @date 2024-01-05 13:25:18
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    /**
     * 获取分类下所有分组关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R catelogWithattr(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupWithAttrVo> attrGroupWithAttrVos= attrGroupService.catelogWithattr(catelogId);

        return R.ok().put("data",attrGroupWithAttrVos);
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params,
                  @PathVariable("attrgroupId") Long attrgroupId){

        PageUtils page = attrGroupService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",page);
    }


    /**
     * 添加属性与分组的关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] vos){
        attrGroupService.deleteRelation(vos);

        return R.ok();
    }


    /**
     * 属性分组关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){

        List<AttrEntity> attrEntityList = attrService.getByAttrGroupId(attrgroupId);

        return R.ok().put("data", attrEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
